home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- Copyright (c) 2008 Ensolis, LLC. All Rights Reserved.
- ----------------------------------------------------------------------------*/
-
- /******************************************************************************
- * WARNING!!! This file cannot be used from the main overlay (forecastfox.js).
- * There are variables and functions that conflict with browser
- * code and could potentially conflict with other extensions.
- *****************************************************************************/
-
- /******************************************************************************
- * Component Constants
- *****************************************************************************/
- const Cc = Components.classes;
- const Ci = Components.interfaces;
- const Cr = Components.results;
-
- /******************************************************************************
- * File Permission Constants
- *****************************************************************************/
- /*jsl:ignore*/
- const PERMS_FILE = 0644;
- const PERMS_DIRECTORY = 0755;
- /*jsl:end*/
-
- /******************************************************************************
- * File Type Constants
- *****************************************************************************/
- const TYPE_PROFILE = Ci.ffIDiskService.TYPE_PROFILE;
- const TYPE_CACHE = Ci.ffIDiskService.TYPE_CACHE;
- const TYPE_ICONS = Ci.ffIDiskService.TYPE_ICONS;
- const TYPE_TEMP = Ci.ffIDiskService.TYPE_TEMP;
- const TYPE_DEFAULTS = Ci.ffIDiskService.TYPE_DEFAULTS;
- const TYPE_WEATHERFOX = Ci.ffIDiskService.TYPE_WEATHERFOX;
- const TYPE_ERRORS = Ci.ffIDiskService.TYPE_ERRORS;
-
- /******************************************************************************
- * Severity Constants
- *****************************************************************************/
- const SEVERITY_INFO = Ci.ffIErrorItem.SEVERITY_INFO;
- const SEVERITY_WARNING = Ci.ffIErrorItem.SEVERITY_WARNING;
- const SEVERITY_ERROR = Ci.ffIErrorItem.SEVERITY_ERROR;
-
- const ONE_SECOND = 1000;
- const ONE_MINUTE = 60*ONE_SECOND;
- const ONE_HOUR = 60*ONE_MINUTE;
- const ONE_DAY = 24*ONE_HOUR;
- const ONE_WEEK = 7*ONE_DAY;
- const ONE_MONTH = 4*ONE_WEEK;
-
- /******************************************************************************
- * Preferences Excluded from Profiles Constant
- *****************************************************************************/
- const EXCLUDED_PREFS = {
- "migrated": true,
- "migrated.prefs": true,
- "pinged": true,
- "icons.version": true,
- "icons.uninstallfiles": true,
- "onetime.chromepromo": true,
- "profile.current": true,
- "profile.switch.delay": true,
- "profile.switch.enabled": true,
- "links.alert": true,
- "links.dialog": true,
- "links.panel": true,
- "links.context": true,
- "pinged.daily": true,
- "pinged.weekly": true,
- "pinged.monthly": true,
- "debug": true
- };
-
- /******************************************************************************
- * DTD and Namespace Constants for Import, Export, and Profiles.xml
- *****************************************************************************/
- const PROFILES_DTD = "http://forecastfox.ensolis.com/specs/1.0/profiles.dtd";
- const PROFILES_NS = "http://forecastfox.ensolis.com/specs/1.0/profiles";
-
- /******************************************************************************
- * Gets a preference branch.
- *
- * @param Boolean if getting the default branch or current branch.
- * @param Name of the branch to get. If null is passed then "forecastfox."
- * is the branch retrieved.
- * @return Requested preference branch.
- *****************************************************************************/
- function getBranch(aDefault, aName)
- {
- //forecastf pref branch
- const FF_NAME = "forecastfox.";
-
- //get pref service
- var pbSvc = Cc["@mozilla.org/preferences-service;1"].
- getService(Ci.nsIPrefService);
-
- //get the default branch
- if (aDefault)
- return (aName) ? pbSvc.getDefaultBranch(aName) :
- pbSvc.getDefaultBranch(FF_NAME);
-
- //get the specified branch
- return (aName) ? pbSvc.getBranch(aName) :
- pbSvc.getBranch(FF_NAME);
- }
-
- /******************************************************************************
- * Gets a preference.
- *
- * @param Name of the preference to retrieve.
- * @return Requested preference value.
- *****************************************************************************/
- var gHelpersBranch = null;
- function getPref(aName)
- {
- //get pref branch
- if (!gHelpersBranch)
- gHelpersBranch = getBranch(false, null);
- var branch = gHelpersBranch;
-
- //return value based on pref type
- var rv = "";
- switch (branch.getPrefType(aName)) {
- case Ci.nsIPrefBranch.PREF_INT:
- rv = branch.getIntPref(aName);
- break;
- case Ci.nsIPrefBranch.PREF_BOOL:
- rv = branch.getBoolPref(aName);
- break;
- case Ci.nsIPrefBranch.PREF_STRING:
- default:
- try {
- rv = branch.getComplexValue(aName, Ci.nsIPrefLocalizedString).data;
- } catch(e) {
- try {
- rv = branch.getComplexValue(aName, Ci.nsISupportsString).data;
- } catch(e) {
- rv = branch.getCharPref(aName);
- }
- }
- break;
- }
- return rv;
- }
-
- /******************************************************************************
- * Sets a preference.
- *
- * @param Name of the preference to retrieve.
- * @param Value to set the preference to.
- *****************************************************************************/
- function setPref(aName, aValue)
- {
- //get pref branch
- if (!gHelpersBranch)
- gHelpersBranch = getBranch(false, null);
- var branch = gHelpersBranch;
-
- //do nothing if value is unchanged
- var oldValue = getPref(aName);
- if (aValue == oldValue)
- return;
-
- //remove user value if same as the default
- var restored = restorePref(aName, aValue);
- if (restored)
- return;
-
- //set value based on pref type
- switch (branch.getPrefType(aName)) {
- case Ci.nsIPrefBranch.PREF_INT:
- branch.setIntPref(aName, aValue);
- break;
- case Ci.nsIPrefBranch.PREF_BOOL:
- branch.setBoolPref(aName, aValue);
- break;
- case Ci.nsIPrefBranch.PREF_STRING:
- default:
- try {
- var plString = Cc["@mozilla.org/pref-localizedstring;1"].
- createInstance(Ci.nsIPrefLocalizedString);
- plString.data = aValue;
- branch.setComplexValue(aName, Ci.nsIPrefLocalizedString, plString);
- } catch(e) {
- try {
- var sString = Cc["@mozilla.org/supports-string;1"].
- createInstance(Ci.nsISupportsString);
- sString.data = aValue;
- branch.setComplexValue(aName, Ci.nsISupportsString, sString);
- } catch(e) {
- branch.setCharPref(aName, aValue);
- }
- }
- break;
- }
- }
-
- /******************************************************************************
- * Restores a default preference.
- *
- * @param Name of the preference to retrieve.
- * @param The new value of the preference.
- * @return True if pref was restored
- *****************************************************************************/
- function restorePref(aName, aValue)
- {
- //get pref branch
- var branch = getBranch(true, null);
-
- //get value based on pref type
- try {
- var defaultValue = "";
- switch (branch.getPrefType(aName)) {
- case Ci.nsIPrefBranch.PREF_INT:
- defaultValue = branch.getIntPref(aName);
- break;
- case Ci.nsIPrefBranch.PREF_BOOL:
- defaultValue = branch.getBoolPref(aName);
- break;
- case Ci.nsIPrefBranch.PREF_STRING:
- default:
- try {
- defaultValue = branch.getComplexValue(aName, Ci.nsIPrefLocalizedString).data;
- } catch(e) {
- try {
- defaultValue = branch.getComplexValue(aName, Ci.nsISupportsString).data;
- } catch(e) {
- defaultValue = branch.getCharPref(aName);
- }
- }
- break;
- }
- } catch(e) {
- return false;
- }
-
- //value is the same do not restore
- if (aValue != defaultValue)
- return false;
-
- //get pref branch
- if (!gHelpersBranch)
- gHelpersBranch = getBranch(false, null);
- branch = gHelpersBranch;
-
- //clear the value
- try {
- branch.clearUserPref(aName);
- } catch(e) {
- return false;
- }
-
- // preference was cleared
- return true;
- }
-
- /******************************************************************************
- * removes the specified file
- *
- * @param File to remove. If file is a directory all files within the
- * directory will be removed.
- *****************************************************************************/
- function removeFile(aFile)
- {
- if (aFile.isDirectory())
- aFile.remove(true);
- else
- aFile.remove(false);
- }
-
- /******************************************************************************
- * Gets a directory based on a special directory key.
- *
- * @param Special directory key.
- * @param Array of sub directories from the special directory.
- * @param Create the sub directory if it doesn't exist.
- * @return A nsIFile interface for the requested file.
- *****************************************************************************/
- function getKeyedDirectory(aKey, aPathArray, aCreate)
- {
- //get directory service
- var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
- getService(Ci.nsIProperties);
-
- //get base directory
- var dir = dirSvc.get(aKey, Ci.nsIFile);
-
- //loop through path array
- for (var i=0; i<aPathArray.length; i++) {
- dir.append(aPathArray[i]);
-
- //create directory if instructed
- if (aCreate && !dir.exists())
- dir.create(Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
- }
-
- return dir;
- }
-
- /******************************************************************************
- * Gets a directory from the installed directory.
- *
- * @param Array of sub directories from the install directory.
- * @return A nsIFile interface for the requested file.
- *****************************************************************************/
- function getInstallDirectory(aPathArray)
- {
- //setup objects
- var dir = null;
-
- //get extension manager - toolkit 1.0 or greater
- if ("@mozilla.org/extensions/manager;1" in Cc) {
- var em = Cc["@mozilla.org/extensions/manager;1"].
- getService(Ci.nsIExtensionManager);
-
- //get install location from extension manager - toolkit 1.5
- if ("nsIInstallLocation" in Ci) {
- dir = em.getInstallLocation("{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}");
- dir = dir.getItemLocation("{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}");
- }
- }
-
- //couldn't use extension manager so try the profile directory - non toolkit
- if (!dir) {
- var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
- getService(Ci.nsIProperties);
- dir = dirSvc.get("ProfD", Ci.nsIFile);
- dir.append("extensions");
- dir.append("{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}");
-
- //not in the profile directory so it must be in the app directory
- if (!dir.exists()) {
- dir = dirSvc.get("XCurProcD", Ci.nsIFile);
- dir.append("extensions");
- dir.append("{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}");
- }
- }
-
- //loop through path array
- for (var i=0; i<aPathArray.length; i++)
- dir.append(aPathArray[i]);
-
- return dir;
- }
-
- /******************************************************************************
- * Get the top most open window. A window has to open for this to be called.
- * Use this if the type of window does not matter.
- *
- * @return A window object used for modality.
- *****************************************************************************/
- function getTopWindow()
- {
- //get top window
- var mediator = Cc["@mozilla.org/appshell/window-mediator;1"].
- getService(Ci.nsIWindowMediator);
- return mediator.getMostRecentWindow(null);
- }
-
- /******************************************************************************
- * Get the main application window. Use this if the type of window does matter.
- *
- * @return A window object used for modality.
- *****************************************************************************/
- function getMainWindow()
- {
- /** this may need to change if main window of a
- supported app is not "navigator:browser" **/
-
- //get the mediator service
- var mediator = Cc["@mozilla.org/appshell/window-mediator;1"].
- getService(Ci.nsIWindowMediator);
-
- //get the app window
- var main = mediator.getMostRecentWindow("navigator:browser");
- if (main)
- return main;
-
- //get the watcher service
- var watcher = Cc["@mozilla.org/embedcomp/window-watcher;1"].
- getService(Ci.nsIWindowWatcher);
-
- //open a new window
- main = watcher.openWindow(null, "chrome://browser/content/browser.xul",
- "_blank", "chrome,all,dialog=no", "about:blank");
- return main;
- }
-
- /******************************************************************************
- * String enumerator of hash table keys.
- *
- * @param Javascript hash table.
- * @return A nsIStringEnumerator of the keys.
- *****************************************************************************/
- function KeyEnumerator(aHashTable)
- {
- //setup key array
- this._keys = [];
- this._index = 0;
-
- //load with data
- if (aHashTable) {
- for (var name in aHashTable)
- this._keys.push(name);
- }
- }
- KeyEnumerator.prototype = {
- _index: null,
- _keys: null,
-
- QueryInterface: function KeyEnumerator_QueryInterface(aIID)
- {
- if (!aIID.equals(Ci.nsIStringEnumerator) ||
- !aIID.equals(Ci.nsISupports))
- throw Cr.NS_ERROR_NO_INTERFACE;
- return this;
- },
-
- hasMore: function KeyEnumerator_hasMore()
- {
- return this._index < this._keys.length;
- },
-
- getNext: function KeyEnumerator_getNext()
- {
- var rv = this._keys[this._index];
- this._index++;
- return rv;
- }
- };
-
- /******************************************************************************
- * Sorts an array ascending where the items have a name property.
- *
- * @param Current array item.
- * @param Next array item.
- *
- * @return 1 if greater, 0 if equal, and -1 if less than.
- *****************************************************************************/
- function sortByName(aItem1, aItem2)
- {
- if (aItem1.name < aItem2.name)
- return -1;
- else if (aItem1.name == aItem2.name)
- return 0;
-
- return 1;
- }
-
- /******************************************************************************
- * Get a new prompter.
- *
- * @param The parent window for the prompter can be null.
- *
- * @return A new prompter.
- *****************************************************************************/
- function getPrompter(aParent)
- {
- //get the watcher service
- var watcher = Cc["@mozilla.org/embedcomp/window-watcher;1"].
- getService(Ci.nsIWindowWatcher);
-
- //return a prompter
- return watcher.getNewPrompter(aParent);
- }
-
- /******************************************************************************
- * Gets the forecastfox string bundle.
- *
- * @return A nsIStringBundle interface for the requested url.
- *****************************************************************************/
- var gHelpersBundle = null;
- function getBundle()
- {
- if (gHelpersBundle != null)
- return gHelpersBundle;
-
- const BUNDLE_URL = "chrome://forecastfox/locale/forecastfox.properties";
-
- //get the stringbundle service
- var sbSvc = Cc["@mozilla.org/intl/stringbundle;1"].
- getService(Ci.nsIStringBundleService);
-
- //get the bundle and return it
- gHelpersBundle = sbSvc.createBundle(BUNDLE_URL);
- return gHelpersBundle;
- }
-
- /******************************************************************************
- * Checks if the alert service is included.
- *
- * @return True if alert service is present.
- *****************************************************************************/
- var gHelpersHasAlert = null;
- function checkAlertService()
- {
- //return cached alert check
- if (gHelpersHasAlert != null)
- return gHelpersHasAlert;
-
- //check if the alert interface exists
- if ("nsIAlertsService" in Ci)
- gHelpersHasAlert = true;
- else
- gHelpersHasAlert = false;
-
- //cache the flag and return the value
- return gHelpersHasAlert;
- }
-
- /******************************************************************************
- * Open a link in the main application window
- *
- * @param The url to open.
- * @param Where to open the link (current, window, tab, tabshifted)
- *****************************************************************************/
- function openLink(aURL, aWhere)
- {
- var win = getMainWindow();
- var browser = win.document.getElementById("content");
- var features = "chrome,all,dialog=no";
- var chrome = "";
- switch (aWhere) {
-
- //open in a new window
- case "window":
- chrome = "chrome://browser/content/browser.xul";
- win.openDialog(chrome, "_blank", features, aURL, null, null);
- break;
-
- //open in a new tab
- case "tab":
- case "tabshifted":
- var tab = browser.addTab(aURL);
-
- //focus the tab
- if (aWhere == "tab") {
- browser.selectedTab = tab;
- win.content.focus();
- }
- break;
-
- //open in the current tab
- case "current":
- default:
- browser.loadURI(aURL);
- win.content.focus();
- break;
- }
- }
-
- /******************************************************************************
- * write a message to the console and to stdout
- *
- * @param Values to log.
- *****************************************************************************/
- function LOG(values) {
- var debug = getPref("debug");
- if (debug === true) {
- var msg =(values.join ? values.join("") : values);
- console = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
- console.logStringMessage(msg);
- dump(msg + "\n");
- }
- }